clc;clear;

%-------------------------------Data----------------------------%
V1 = 1 + 0j;
V2 = 1 + 0j;
V3 = 1 + 0j;

P1 = 0;
Q1 = 0j;

P2 = -400/100;
Q2 = -320j/100;

P3 = -300/100;
Q3 = -270j/100;

z12 =  0.0333j;
z13 =  0.0125j;
z23 =  0.05j;
%-----------------------%

%------------------------Admitance------------------------------%
y12 = 1/z12;
y13 = 1/z13;
y23 = 1/z23;

Y = zeros(3,3);
Y(1,1) = y12 + y13;
Y(1,2) = -y12;
Y(1,3) = -y13;

Y(2,1) = Y(1,2);
Y(2,2) = y12 + y23;
Y(2,3) = -y23;

Y(3,1) = Y(1,3);
Y(3,2) = Y(2,3);
Y(3,3) = y13 + y23;
%----------------------%

%---------------------apparent power---------------------------%
S1 = P1 + Q1;
S3 = P2 + Q2;
S3 = P2 + Q3;
%----------------------%

%---------------------------iterations-------------------------%
for i = 1:1
V2 = (1/(Y(2,2)))*(((P2-Q2)/conj(V2)) - (Y(2,1)*V1 + Y(2,3)*V3))

V3 = (1/(Y(3,3)))*(((P3-Q3)/conj(V3)) - (Y(3,1)*V1 + Y(3,2)*V2));
end 
%---------------------%

S1_1 = P1-Q1;
%------------------------slack bus power-----------------------%
S1_1 = conj(V1)*(V1*Y(1,1)+(V2*Y(1,2)+V3*Y(1,3)));
%---------------------%

%--------------------------currents----------------------------%
I12 = (V1 - V2)*y12;
I21 = -I12;

I13 = (V1 - V3)*y13;
I31 = -I13;

I23 = (V2 - V3)*y23;
I32 = -I23;
%----------------------%

%------------------------appearent power----------------------%
S12 = V1*conj(I12)*100e6;
S21 = V2*conj(I21)*100e6;

S13 = V1*conj(I13)*100e6;
S31 = V3*conj(I31)*100e6;

S23 = V2*conj(I23)*100e6;
S32 = V3*conj(I32)*100e6;
%-----------------------%

%------------------------power losses------------------------%
SL12 = (S12 + S21);
SL13 = (S13 + S31);
SL23 = (S23 + S32);
%----------------------%